'use strict'

const SynkVulnerabilityBase = require('./snyk-vulnerability-base')

module.exports = class SnykVulnerabilityGitHub extends SynkVulnerabilityBase {
  static get route() {
    return {
      base: 'snyk/vulnerabilities/github',
      pattern: ':user/:repo/:manifestFilePath*',
    }
  }

  static get examples() {
    return [
      {
        title: 'Snyk Vulnerabilities for GitHub Repo',
        pattern: ':user/:repo',
        namedParams: {
          user: 'badges',
          repo: 'shields',
        },
        staticPreview: this.render({ vulnerabilities: '0' }),
      },
      {
        title: 'Snyk Vulnerabilities for GitHub Repo (Specific Manifest)',
        pattern: ':user/:repo/:manifestFilePath',
        namedParams: {
          user: 'badges',
          repo: 'shields',
          manifestFilePath: 'gh-badges/package.json',
        },
        staticPreview: this.render({ vulnerabilities: '0' }),
        documentation: `
          <p>
            Provide the path to your target manifest file relative to the base of your repository.
            Snyk does not support using a specific branch for this, so do not include "blob" nor a branch name.
          </p>
          `,
      },
    ]
  }

  async handle({ user, repo, manifestFilePath }) {
    const url = `https://snyk.io/test/github/${user}/${repo}/badge.svg`
    const qs = { targetFile: manifestFilePath }
    const { vulnerabilities } = await this.fetch({
      url,
      qs,
      errorMessages: {
        404: 'repo or manifest not found',
      },
    })
    return this.constructor.render({ vulnerabilities })
  }
}
